package br.com.ifba.sadd.admin.presentation.professor {
	import br.com.ifba.sadd.model.ModelLocator;
	import br.com.ifba.sadd.model.ServiceLocator;

	import mx.controls.Alert;
	import mx.events.ValidationResultEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	[Event(name="reset", type="flash.events.Event")]
	
	[Bindable]
	public class AlterarProfessorPresentationModel extends EventDispatcher
	{
		
		private var _enable:Boolean = false;
		private var _model:ModelLocator = ModelLocator.getInstance();
		private var _service:ServiceLocator = ServiceLocator.getInstance();
		
		public function AlterarProfessorPresentationModel()
		{
		}
		
		/* ----------------- Geters e Seters ------------------*/
		
		public function get model():ModelLocator
		{
			return _model;
		}
		
		public function get enable():Boolean
		{
			return _enable;
		}
		
		public function set enable(value:Boolean):void
		{
			_enable = value;
		}
		
		/* --------------------- Funçõoes de Crud ---------------------*/
		
	
		public function alterar(nome:String, email:String, cpf:String):void
		{
			model.professor.nome = nome.toUpperCase();
			model.professor.cpf = cpf;
			model.professor.email = email.toUpperCase();			
			var token:AsyncToken = _service.professorService.alterarProfessor(model.professor);
			token.addResponder(new Responder(handleAlterar, handleFailure));
			
		}
				
		/* --------------------- Funções de apoio ---------------------*/
		
		public function validarCadastro(nomeValidator:ValidationResultEvent,
										emailValidato:ValidationResultEvent,
										cpfValidador:ValidationResultEvent):void
		{
			if(nomeValidator.type == ValidationResultEvent.VALID &&
			   emailValidato.type == ValidationResultEvent.VALID &&
			   cpfValidador.type == ValidationResultEvent.VALID){
				enable = true;
			}
			else{
				enable = false;
			}
		}
		
		/* --------------------- handles ---------------------*/
		
		private function handleFailure(event:FaultEvent):void
		{
			Alert.show(event.fault.faultString, "Erro!");
		}
		
		
		private function handleAlterar(event:ResultEvent):void
		{
			dispatchEvent(new Event("reset"));
			Alert.show("Cadastro alterado com sucesso");
		}
			
	}
}